<template>
	<el-dialog v-model="showModal" class="peak-dialog" :close-on-click-modal="false" :title="title" :width="1200">
		<static-table :tableData="tableData" rowId="configId" :loading="loading" :seqConfig="{ startIndex: (pageNum - 1) * pageSize }">
			<template #vux-column>
				<vxe-column type="seq" title="序号" width="60" />
				<vxe-column field="modelKey" min-width="150" show-overflow="tooltip" title="模型标识" />
				<vxe-column field="modelName" min-width="120" show-overflow="tooltip" title="模型名称" />
				<vxe-column field="category" :formatter="formatterCategory" min-width="120" title="流程分类" />
				<vxe-column field="version" min-width="120" title="模型版本">
					<template #default="{ row }">
						<el-tag>v{{ row.version }}</el-tag>
					</template>
				</vxe-column>
				<vxe-column field="createTime" min-width="150" title="创建时间" />
				<vxe-column field="description" min-width="150" show-overflow="tooltip" title="备注" />
				<vxe-column field="operation" fixed="right" min-width="150" title="操作">
					<template #default="{ row }">
						<div class="vxe-action">
							<a @click="handleDeploy(row)" v-has="'handle:workflow:define:deploy'">部署</a>
							<a @click="handleSetNew(row)" v-has="'handle:workflow:define:new'">设为最新</a>
						</div>
					</template>
				</vxe-column>
			</template>
		</static-table>
		<el-pagination background :current-page="pageNum" :layout="layout" :page-size="pageSize" :total="dataCount" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
		<template #footer />
	</el-dialog>
</template>
<script setup>
import global from '@/utils/global'
import useTableList from '@/hooks/useTableList'
/*
 *@Description: 状态初始化
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-10-07 15:34:18
 */
const state = reactive({
	showModal: false,
	title: '',
	searchForm: {
		modelKey: '',
	},
})

const { showModal, title } = toRefs(state)
const queryUrl = '/sop-joint-disposal/web/v1/model/historyList'
const { loading, dataCount, handleInitQuery, getDictKeyText, handleModalOk, tableData, pageNum, pageSize, layout, handleSizeChange, handleCurrentChange } = useTableList(queryUrl, state.searchForm, true, false)

/*
 *@Description: 方法模块
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-10-07 15:34:24
 */
const handleSearch = () => {
	handleInitQuery()
}
const handleShowModal = async row => {
	state.showModal = true
	await nextTick()
	state.searchForm.modelKey = row.modelKey
	state.title = '流程模型历史'
	handleSearch()
}
const formatterCategory = ({ cellValue }) => {
	return getDictKeyText('model_category', cellValue)
}
const handleSetNew = row => {
	global.$baseConfirm(
		`是否确认设置名称为"--【${row.modelName}】--"流程为最新流程?`,
		'提示',
		() => {
			global.$fetchPostData(`/sop-joint-disposal/web/v1/model/latest?modelId=${row.modelId}`, {}).then(data => {
				if (data.code == 200) {
					global.$baseMessage(data.msg, 'success')
					handleModalOk(2)
				} else {
					global.$baseMessage(data.msg, 'error')
				}
			})
		},
		() => {}
	)
}
const handleDeploy = row => {
	global.$baseConfirm(
		`是否确认部署名称为"--【${row.modelName}】--"的流程?`,
		'提示',
		() => {
			global.$fetchPostData(`/sop-joint-disposal/web/v1/model/deployModel?modelId=${row.modelId}`, {}).then(data => {
				if (data.code == 200) {
					global.$baseMessage(data.msg, 'success')
					handleModalOk(2)
				} else {
					global.$baseMessage(data.msg, 'error')
				}
			})
		},
		() => {}
	)
}
defineExpose({
	handleShowModal,
})
</script>
<style lang="scss" scoped></style>
